#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py
# Date    : 2021-02-05
# Author  : Tom.Lee

import requests

# 去除warning
requests.packages.urllib3.disable_warnings()

url = "https://testpassport.rabbitpre.com/api/sso/login"
bws_url = "https://bws.test.rabbitpre.com/api/user"
home_url = "https://home.test.rabbitpre.com/api/workbench/org/getCompanyInfo"

login_info = {"account":"xxxx","password":"xxx","fromType":"pc"}



def login():
    sess = requests.session()
    resp = sess.post(url, json=login_info, verify=False)
    if "disposableCode" not in resp.text:
        raise ValueError("登录失败")
    resp = sess.get(bws_url, verify=False)
    if "userState" not in resp.text:
        raise ValueError("bws同步失败")
    return sess

def main():
    try:
        sess = login()
        resp = sess.get(home_url, verify=False)
        orgid = resp.json()["data"]["org_id"]
        userid = resp.json()["data"]["org_user"]
    except KeyError as e:
        print(e)
    except ValueError as e:
        print(e)
    except Exception as e:#匹配了所有异常，不建议使用此方法
        print(e)
    else:
        print("userid is {}".format(userid))
        print("orgid is {}".format(orgid))


if __name__ == '__main__':
    main()